home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / chess / fics / style12.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  5.5 KB  |  206 lines

  1. # -*- coding: utf-8 -*-
  2. import re
  3.  
  4. """See: http://www.unix-ag.uni-kl.de/~chess/gicshelp/node197.html"""
  5.  
  6. COLOUR_BLACK = 'B'
  7. COLOUR_WHITE = 'W'
  8.  
  9. RELATIONSHIP_PLAYING_OPPONENT_MOVE = '-1'
  10. RELATIONSHIP_PLAYING_MY_MOVE       = '1'
  11. RELATIONSHIP_OBSERVER_EXAMINED     = '-2'
  12. RELATIONSHIP_OBSERVER              = '0'
  13. RELATIONSHIP_EXAMINER              = '2'
  14.  
  15. #<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 89 GuestGLQF GuestPSVJ 1 2 12 39 39 120 120 1 none (0:00) none 0 0 0
  16.  
  17. #<12> 
  18. #rnbqkbnr 
  19. #pppppppp 
  20. #-------- 
  21. #-------- 
  22. #-------- 
  23. #-------- 
  24. #PPPPPPPP 
  25. #RNBQKBNR 
  26. #W 
  27. #-1 
  28. #1 
  29. #1 
  30. #1 
  31. #1 
  32. #0 
  33. #89 
  34. #GuestGLQF 
  35. #GuestPSVJ 
  36. #1 
  37. #2 
  38. #12 
  39. #39 
  40. #39 
  41. #120 
  42. #120 
  43. #1 
  44. #none 
  45. #(0:00) 
  46. #none 
  47. #0 
  48. #0 
  49. #0
  50. movePattern = re.compile('<12> ' +
  51.                          '([prnbqkPRNBQK-]{8}) ' +
  52.                          '([prnbqkPRNBQK-]{8}) ' +
  53.                          '([prnbqkPRNBQK-]{8}) ' +
  54.                          '([prnbqkPRNBQK-]{8}) ' +
  55.                          '([prnbqkPRNBQK-]{8}) ' +
  56.                          '([prnbqkPRNBQK-]{8}) ' +
  57.                          '([prnbqkPRNBQK-]{8}) ' +
  58.                          '([prnbqkPRNBQK-]{8}) ' +
  59.                          '([BW]{1}) ' +             # Colour to move
  60.                          '([-]?\d+) ' +             # Pawn file or -1
  61.                          '([01]{1}) ' +             # Can white short castle
  62.                          '([01]{1}) ' +             # Can white long castle
  63.                          '([01]{1}) ' +             # Can black short castle
  64.                          '([01]{1}) ' +             # Can black long castle
  65.                          '(\d+) ' +                 # Number of irreversable moves
  66.                          '(\d+) ' +                 # Game number
  67.                          '(.+) ' +                  # White name
  68.                          '(.+) ' +                  # Black name
  69.                          '([-]?\d+) ' +             # Relationship to game (-2, 2, -1, 1, 0)
  70.                          '(\d+) ' +
  71.                          '(\d+) ' +
  72.                          '(\d+) ' +                 # White strength
  73.                          '(\d+) ' +                 # Black strength
  74.                          '(\d+) ' +                 # White time
  75.                          '(\d+) ' +                 # Black black
  76.                          '(\d+) ' +                 # Move number
  77.                          '(.+) ' +                  # Move
  78.                          '[(](\d+)[:](\d+)[)] ' +   # Move duration
  79.                          '(.+) ' +                  # Pretty move
  80.                          '([01]{1})' +
  81.                          '([01]{1})' +
  82.                          '([01]{1})')
  83.  
  84. def _decodeFile(string):
  85.     # FIXME: Only allow 'prnbqkPRNBQK-'
  86.     return string
  87.  
  88. def _decodeColour(string):
  89.     if string == 'B' or string == 'W':
  90.         return string
  91.     raise ValueError()
  92.  
  93. def _decodeInteger(string):
  94.     return int(string)
  95.  
  96. def _decodeUInteger(string):
  97.     i = int(string)
  98.     if i < 0:
  99.         raise ValueError()
  100.     return i
  101.  
  102. def _decodeTime(string):
  103.     # FIXME: In form '(mm:ss)'
  104.     return 0
  105.           
  106. def _decodeString(string):
  107.     return string
  108.           
  109. def _decodeBoolean(string):
  110.     if string == '0':
  111.         return False
  112.     if string == '1':
  113.         return True
  114.     raise ValueError()
  115.  
  116. fields = [_decodeFile, _decodeFile, _decodeFile, _decodeFile, _decodeFile, _decodeFile, _decodeFile, _decodeFile,
  117.           _decodeColour, _decodeInteger,
  118.           _decodeBoolean, _decodeBoolean, _decodeBoolean, _decodeBoolean,
  119.           _decodeUInteger, _decodeUInteger,
  120.           _decodeString, _decodeString,
  121.           _decodeInteger,
  122.           _decodeUInteger, _decodeUInteger,
  123.           _decodeUInteger, _decodeUInteger,
  124.           _decodeUInteger, _decodeUInteger,
  125.           _decodeUInteger, _decodeString, _decodeTime, _decodeString,
  126.           _decodeBoolean]
  127.  
  128. class Player:
  129.     """
  130.     """
  131.     name = ''
  132.     canCastleShort = False
  133.     canCastleLong  = False
  134.     strength = 0
  135.     
  136.     # Remaining time
  137.     remaining = 0
  138.  
  139. class Move:
  140.     """
  141.     """
  142.     # The state of the board
  143.     board = None
  144.     
  145.     # The relationship of this reader to the game
  146.     relationship = None
  147.     
  148.     # The game number
  149.     gameNumber = 0
  150.     startTime = 0 # s
  151.     increment = 0 # ms
  152.     
  153.     # ?
  154.     nReversibleMoves = 0
  155.     
  156.     # The player states
  157.     white = None
  158.     black = None
  159.     
  160.     # The previous move
  161.     move       = ''
  162.     movePretty = ''
  163.     marchFile  = -1
  164.     moveTime   = 0 # seconds
  165.     
  166.     # The number of the move to make
  167.     moveNumber = 0
  168.     colourToMove = None
  169.     
  170.     # ?
  171.     flip = None
  172.  
  173.     def __init__(self):
  174.         self.white = Player()
  175.         self.black = Player()
  176.  
  177. def decode(line):
  178.     """
  179.     """
  180.     tokens = line.split()[1:]
  181.     if len(tokens) < len(fields):
  182.         raise ValueError('Too few fields')
  183.  
  184.     out = []
  185.     for i in xrange(len(fields)):
  186.         try:
  187.             out.append(fields[i](tokens[i]))
  188.         except ValueError:
  189.             raise ValueError('Invalid field: %s' % tokens[i])
  190.  
  191.     m = Move()
  192.     (file1, file2, file3, file4, file5, file6, file7, file8,
  193.      moveColour, pushFile,
  194.      whiteCastleShort, whiteCastleLong, blackCastleShort, blackCastleLong,
  195.      nIrreversableMoves, m.gameNumber,
  196.      m.white.name, m.black.name,
  197.      m.relationship,
  198.      initialTime, incrementTime,
  199.      m.white.strength, m.black.strength,
  200.      m.white.remaining, m.black.remaining,
  201.      m.moveNumber, m.move, m.moveTime, m.movePretty,
  202.      orientation) = out
  203.     m.board = (file1, file2, file3, file4, file5, file6, file7, file8)
  204.  
  205.     return m
  206.